<?php

	session_start();

	header("Content-Type: text/html; charset=UTF-8");

	require_once("appconf.php");


	$page 		= "dashboard"; //Default
	$validPages 	= array("login", "dashboard", "template_builder", "template_executor", "template_set_variables");
	$scripts 	= array("template_builder"
					=> array("js/prototype.js", "js/utility.js", "js/utility.EventHandler.js", "js/utility.TypeRendering.js", "js/types.js.php", "js/queries.js.php", "js/template_builder.js", "js/template_builder.queries.js", "js/template_builder.variables.js"),
				"dashboard"
					=> array("js/prototype.js", "js/tooltip.js", "js/utility.js", "js/dashboard.js"),
				"template_set_variables"
					=> array("js/prototype.js", "js/utility.js", "js/utility.EventHandler.js", "js/utility.TypeRendering.js", "js/types.js.php", "js/queries.js.php", "js/template_set_variables.js")
				);


	if (isset($_REQUEST["page"])) {
		if (in_array($_REQUEST["page"], $validPages))
			$page = $_REQUEST["page"];
	}



	if (isset($_POST["login"])) {
		$username = $_POST["username"];
		$password = $_POST["password"];

		if ($userValidator->validate($username, $password)) {
			$_SESSION["username"] = $username;
		} else {
			//XXX Show error
		}
	}

	if (isset($_REQUEST["logout"])) {
		unset($_SESSION["username"]);
	}



	if (!isset($_SESSION["username"]))
		$page = "login";

?>

<html>

	<head>

		<title>
			Bluebird Reports
		</title>

		<link href="css/app.css" type="text/css" rel="stylesheet" />

<?php
		if (isset($scripts[$page])) {
			for ($i=0; $i<sizeof($scripts[$page]); $i++) {
				echo("\t\t<script src=\"".$scripts[$page][$i]."\" type=\"text/javascript\"></script>\n");
			}
		}
?>

	</head>

	<body>

		<div id="main_container">

			<table>
				<tr>
					<td>
						<a href="./">
							<img src="img/bluebird.jpg" style="border:0px;" />
						</a>
					</td>
					<?php
						if (isset($_SESSION["username"])) {
							echo "<td style=\"width:20px;\">";
							echo "</td>";
							echo "<td>";
							echo "<a href=\"./\">Dashboard</a>";
							echo "&nbsp;";
							echo "&nbsp;";
							echo "&nbsp;";
							echo "<a href=\"./?logout=1\">Log out</a>";
							echo "</td>";
						}
					?>
				</tr>
			</table>

			<?php
			require_once($page.".php");
			?>

		</div>

	</body>

</html>